package com.itmate.chenshiguang.dao;

import java.util.ArrayList;
import java.util.List;

import com.itmate.chenshiguang.db.DBUtil;
import com.itmate.chenshiguang.entity.account;


public class AccountDao extends BaseDao {

	
	/**
	 * 判断用户名密码对应的数据是否存在
	 * @param pu
	 * @return 返回 true 存在， false不存在
	 */
	public boolean isExist(account pu){
		boolean flag = false;
		 try {
			 	int count = 0;
				connection = DBUtil.getConnection();
				String sql = "select count(*) as count from account where date=? and money=?";
				preparedStatement = connection.prepareStatement(sql);
				preparedStatement.setString(1, pu.getDate());
				preparedStatement.setDouble(2, pu.getMoney());
				resultSet = preparedStatement.executeQuery();
				while(resultSet.next()){
					//如果可以查找到id那么就说明该数据存在
					count = resultSet.getInt("count");
				}
				if (count != 0 ){
					flag = true;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				closed();
			}
		 return flag;
	}
	
	/**
	 * 查询所有用户
	 * @return
	 */
	public List<account> selectAllUser(){
		List<account> accountlist = new ArrayList<>();
		try {
		 	
			connection = DBUtil.getConnection();
			String sql = "select * from account";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()){
				account pu = new account();
				pu.setId(resultSet.getInt("id"));
				pu.setStatue(resultSet.getString("statue"));
				pu.setMoney(resultSet.getDouble("money"));
				pu.setDate(resultSet.getString("date"));
				pu.setFamily_id(resultSet.getInt("family_id"));
				pu.setMember_id(resultSet.getInt("member_id"));
				accountlist.add(pu);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		return accountlist;
	}
	public List<account> selectMoney(){
		List<account> list = new ArrayList<>();
		try{
			connection = DBUtil.getConnection();
			String sql = "select id from money";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()){
				account p = new account();
				p.setMoney(resultSet.getDouble("money"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			closed();
		}
		return  list;
		
		
	}

}
